package com.java2007.zhangyu.restaurant.dao.impl;

import com.java2007.zhangyu.restaurant.dao.FoodTypeDao;
import com.java2007.zhangyu.restaurant.pojo.DinnerTable;
import com.java2007.zhangyu.restaurant.pojo.FoodType;
import com.java2007.zhangyu.restaurant.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 菜类别持久层实现类
 * @author zy
 * @Version
 * @date 2021/01/22
 */
public class FoodTypeDaoImpl implements FoodTypeDao {
    @Override
    public int saveByFoodTypeName(String fTypeName) throws SQLException{

        QueryRunner runner = new QueryRunner();

        String sql = "INSERT INTO t_food_type(ft_name) VALUES(?)";

        return runner.update(JdbcUtils.getConnection(),sql,fTypeName);

    }

    @Override
    public int deleteByFoodTypeId(Integer fTypeID) throws SQLException{
        QueryRunner runner = new QueryRunner();

        String sql = "DELETE FROM t_food_type WHERE ft_id = ?";

        return runner.update(JdbcUtils.getConnection(),sql,fTypeID);
    }

    @Override
    public int updateByFoodTypeName(FoodType foodType) throws SQLException {
        QueryRunner runner = new QueryRunner();

        String sql = "UPDATE t_food_type SET ft_name = ? WHERE ft_id = ?";

        return runner.update(JdbcUtils.getConnection(),sql,foodType.getFtName(),foodType.getFtId());
    }

    @Override
    public FoodType queryByFoodTypeId(Integer fTypeId) throws SQLException {
        QueryRunner runner = new QueryRunner();

        String sql = "SELECT ft_id ftId, ft_name ftName FROM t_food_type WHERE ft_id = ?";

        return runner.query(JdbcUtils.getConnection(),sql,new BeanHandler<>(FoodType.class),fTypeId);
    }

    @Override
    public List<FoodType> queryByFoodTypeName(String fTypeName) throws SQLException {
        QueryRunner runner = new QueryRunner();

        String sql = "SELECT ft_id ftId, ft_name ftName FROM t_food_type WHERE ft_name like ?";

        return runner.query(JdbcUtils.getConnection(),sql,new BeanListHandler<>(FoodType.class),fTypeName);
    }

    @Override
    public List<FoodType> queryAll() throws SQLException {
        QueryRunner runner = new QueryRunner();

        String sql = "SELECT ft_id ftId,ft_name ftName FROM t_food_type";

        return runner.query(JdbcUtils.getConnection(),sql,new BeanListHandler<>(FoodType.class));
    }

    @Override
    public FoodType selectFoodTypeByDtName(String fTypeName) throws Exception {
        QueryRunner runner = new QueryRunner();

        String sql = "SELECT ft_id ftId,ft_name ftName FROM t_food_type WHERE ft_name = ?";

        return runner.query(JdbcUtils.getConnection(),sql,new BeanHandler<>(FoodType.class),fTypeName);
    }
}
